package com.hyyt.shopjsp.home.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.home.pojo.ShopHomeMiddleCategoryTab;
import com.hyyt.shopjsp.home.service.IShopHomeMiddleCategoryTabService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/03/28 0019下午 2:41.
 */
@Controller
@RequestMapping("/back/shopHomeMiddleCategoryTAB")
public class ShopHomeMiddleCategoryTabController extends BaseController {
    private Logger logger = Logger.getLogger(ShopHomeMiddleCategoryTabController.class);

    @Resource
    private IShopHomeMiddleCategoryTabService shopHomeMiddleCategoryTabService;//首页中间分类中间维护service

	/**
	 * 查询所有首页中间分类中间维护
	 * @param categoryId   分类id
	 * @param page
	 * @param response
	 * @param title			主题
	 * @param isShow		是否显示
	 * @param imageType		图片类型
	 */
	@RequestMapping(value="listShopHomeMiddleCategoryTAB",method=RequestMethod.POST)
	@ResponseBody
	public void listShopHomeMiddleCategoryTAB(String categoryId,String title,String isShow,String imageType,Page page, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> param = new HashMap<>();
		param.put("categoryId", categoryId);
		if(Utils.objectIsNotEmpty(title)){
			param.put("title", title.trim());
		}
		if(Utils.objectIsNotEmpty(isShow)){
			param.put("isShow", isShow);
		}
		if(Utils.objectIsNotEmpty(imageType)){
			param.put("imageType", imageType);
		}
		param.put("sortColumns", " sortCode asc");
		int totalRecordCount =shopHomeMiddleCategoryTabService.countShopHomeMiddleCategoryTab(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopHomeMiddleCategoryTab> list =shopHomeMiddleCategoryTabService.queryShopHomeMiddleCategoryTab(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据首页中间分类中间维护id获得对象
     *
     * @param categoryTabId
     */
    @RequestMapping(value = "getShopHomeMiddleCategoryTABObj", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getShopHomeMiddleCategoryTABObj(Integer categoryTabId) {
		if (Utils.objectIsNotEmpty(categoryTabId)) {
			ShopHomeMiddleCategoryTab categoryTab = shopHomeMiddleCategoryTabService.loadShopHomeMiddleCategoryTab(categoryTabId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(categoryTab));
		}
		return ResultMsg.create(false);
	}

    /**
     * 保存或者修改首页中间分类中间维护
     *
     * @param categoryTab
     * @return
     */
    @RequestMapping(value = "saveOrUpdateShopHomeMiddleCategoryTAB", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateShopHomeMiddleCategoryTAB(ShopHomeMiddleCategoryTab categoryTab) {
		if (Utils.objectIsNotEmpty(categoryTab)) {
			BasicUsers basicUsers = getSessionUsers();
			if (Utils.objectIsEmpty(categoryTab.getCategoryTabId())) {
				Timestamp createTime = new Timestamp(System.currentTimeMillis()); 
				categoryTab.setCreateTime(createTime);
				categoryTab.setPublishUser(basicUsers.getUserName());
				shopHomeMiddleCategoryTabService.saveShopHomeMiddleCategoryTab(categoryTab);
			} else {
				Timestamp updateTime = new Timestamp(System.currentTimeMillis()); 
				categoryTab.setUpdateTime(updateTime);
				categoryTab.setModifyUser(basicUsers.getUserName());
				shopHomeMiddleCategoryTabService.updateShopHomeMiddleCategoryTab(categoryTab);
			}
			return ResultMsg.create(true);
		}else{
			return ResultMsg.create(false);
		}
	}

    /**
     * 根据传入的首页中间分类中间维护id删除
     *
     * @param categoryTabId
     */
    @RequestMapping(value = "deleteHomeMiddleCategoryTAB", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteHomeMiddleCategoryTAB(Integer categoryTabId) {
		if (Utils.objectIsNotEmpty(categoryTabId)) {
			shopHomeMiddleCategoryTabService.deleteShopHomeMiddleCategoryTab(categoryTabId);
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}
}
